package com.xzp.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzp.yygh.common.result.Result;
import com.xzp.yygh.common.util.MD5;
import com.xzp.yygh.hosp.service.HospitalSetService;
import com.xzp.yygh.model.hosp.HospitalSet;
import com.xzp.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(tags = "医院设置管理")
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

//    localhost:8201/admin/hosp/hospitalSet/findAll
    // 获取所有医院信息
    @GetMapping("findAll")
    @ApiOperation(value = "获取所有医院信息")
    public Result findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        if(list != null){
            return Result.ok(list);
        }else{
            return Result.fail();
        }

    }

    // 根据id逻辑删除医院信息
    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id逻辑删除医院信息")
    public Result removeHospitalSet(@PathVariable long id){
        boolean b = hospitalSetService.removeById(id);
        if (b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    // 保存HospitalSet
    @ApiOperation(value = "保存HospitalSet")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean b = hospitalSetService.save(hospitalSet);
        if (b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    // 分页， 并获取当前页的数据
    @ApiOperation(value = "根据当前页面和页面大小划分页面， 并获取当前页的数据")
    @PostMapping("findPageHospitalSet/{currentPage}/{limit}")
    public Result findPageHospitalSet(@PathVariable(name = "currentPage") Long currentPage, @PathVariable(name = "limit") Long limit,
                                      @RequestBody (required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> userPage = new Page<HospitalSet>(currentPage, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname", hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode", hoscode);
        }
        Page<HospitalSet> page = hospitalSetService.page(userPage, wrapper);
        if(page != null && !page.getRecords().isEmpty()){
            return Result.ok(page);
        }else{
            return Result.fail();
        }
    }

    // 批量删除
    @ApiOperation(value = "根据id列表批量移除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<String> idList){
        boolean b = hospitalSetService.removeByIds(idList);
        if (b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //6 修改医院设置
    @ApiOperation(value = "更新医院信息")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 根据id获得HospitalSet
    @ApiOperation(value = "根据id获得HospitalSet")
    @GetMapping("getHospSet/{id}")
    public Result getHospitalSet(@PathVariable Long id) {
        // 测试全局异常捕捉
//        try {
//            System.out.println(1/0);
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new YyghException("查询失败", 201);
//        }
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if(hospitalSet != null) {
            return Result.ok(hospitalSet);
        } else {
            return Result.fail();
        }
    }

    // 锁定医院
    @ApiOperation(value = "锁定医院")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable(value = "id")Long id, @PathVariable(value = "status")Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);
        if(b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    // 发送签名key
    @ApiOperation(value = "发送签名key")
    @GetMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // TODO 发送信息给医院
        return Result.ok();
    }



}
